<template>
  <div class="app-container">
    <el-tabs v-model="queryParams.rdcType" class="tab-box" @tab-click="handleQuery">
      <el-tab-pane label="集团检查" name="DEPT1" v-if="showBtns.dept1" />
      <el-tab-pane label="集群检查" name="DEPT2" v-if="showBtns.dept2" />
    </el-tabs>
    <el-form ref="queryForm" :model="queryParams" @submit.native.prevent size="mini" inline style="white-space: nowrap; overflow-x: auto">
      <el-form-item prop="businessType">
        <el-input v-model.trim="queryParams.businessType" placeholder="请输入检查类型名称" clearable style="width: 160px;" />
      </el-form-item>
      <el-form-item prop="belongSystem">
        <el-select v-model="queryParams.belongSystem" placeholder="请选择所属系统" clearable style="width: 160px;">
          <el-option v-for="dict in dict.type.cq_pe_belong_system" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!--<el-form-item prop="rdcType">
        <el-select v-model="queryParams.rdcType" placeholder="检查单位" clearable style="width: 120px">
          <el-option v-for="dict in dict.type.cq_random_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="showEditDialog">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100vh - 280px)">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="检查类型" show-overflow-tooltip min-width="80">
        <template slot-scope="{ row }">{{ row.businessType }}</template>
      </el-table-column>
      <el-table-column label="所属系统" prop="belongSystem" show-overflow-tooltip min-width="80">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_belong_system" :value="row.belongSystem" />
      </el-table-column>
      <el-table-column label="进入条件" align="center" min-width="150">
        <span slot-scope="{ row }">{{ row.entryConditionName }}</span>
      </el-table-column>
      <el-table-column label="退出条件" align="center" min-width="150">
        <span slot-scope="{ row }">{{ row.exitConditionName }}</span>
      </el-table-column>
      <el-table-column label="检查维度" align="center" show-overflow-tooltip min-width="80">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_dimension" :value="row.dimension" />
      </el-table-column>
      <el-table-column label="频次" align="center" show-overflow-tooltip min-width="70">
        <template slot-scope="{ row }">
          <span>{{ row.frequency }}</span>
          <span>次</span>
          <span v-if="row.frequencyUnit && row.frequencyUnit !== 'EMPTY'">/{{ row.frequencyUnit | dictFilter(dict.type.cq_pe_frequency_unit) }}</span>
          <!--<dict-tag :options="dict.type.cq_pe_frequency_unit" :value="row.frequencyUnit" />-->
        </template>
      </el-table-column>
      <el-table-column label="检查方式" align="center" show-overflow-tooltip min-width="80">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_generation_type" :value="row.generationType" />
      </el-table-column>
      <el-table-column label="状态" align="center" min-width="80">
        <div slot-scope="{ row }" @click="toggleIsEnable(row)" style="display: inline-block; cursor: pointer">
          <i class="el-icon-loading" v-if="row.isEnableLoading" />
          <el-switch class="g-switch-in" v-model="row.isEnable" active-text="启用" inactive-text="停用" :width="50" style="pointer-events: none" v-else />
        </div>
      </el-table-column>
      <el-table-column label="排序" align="center" min-width="60">
        <template slot-scope="{ row }">{{ row.seq }}</template>
      </el-table-column>
      <!-- <el-table-column label="检查单位" align="center" show-overflow-tooltip min-width="80">
        <span slot-scope="{ row }">{{ row.rdcType }}</span>
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_random_type" :value="row.rdcType" />
      </el-table-column>
      <el-table-column label="创建人" prop="creatorName" align="center" show-overflow-tooltip min-width="90" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="150">
        <span slot-scope="{ row }">{{ row.createTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
      </el-table-column>-->
      <el-table-column label="修改人" prop="updatorName" align="center" show-overflow-tooltip min-width="60" />
      <el-table-column label="修改时间" prop="updateTime" align="center" width="150">
        <span slot-scope="{ row }">{{ row.updateTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="210">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)" v-if="showBtns.edit">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="showRelatedItem(row)" v-if="showBtns.relation_list">关联检查项</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-if="showBtns.remove">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />
    <!--关联检查项-->
    <check-item ref="checkItemRef" :rdc-type="queryParams.rdcType" />
  </div>
</template>
<script>
// 工程质量-检查项版本
import { listBsNode, updateBsNode, delBsNode } from '@/api/cqPe/bsNode'
import EditItem from './components/EditBsNode'
import checkItem from './bsChkitem'
import auth from '@/plugins/auth'
import { dictFilter } from '@/utils/filters'
export default {
  name: 'BsNode',
  components: { EditItem, checkItem },
  data() {
    return {
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessType: '',
        belongSystem: '',
        rdcType: 'DEPT1'
      },
      showEditItem: false
    }
  },
  computed: {
    showBtns() {
      // 操作按钮权限
      const deptKey = this.queryParams.rdcType.toLowerCase()
      const edit = auth.hasPermi(`cqPe:bsNode:${deptKey}:edit`)
      const add = auth.hasPermi(`cqPe:bsNode:${deptKey}:add`)
      const remove = auth.hasPermi(`cqPe:bsNode:${deptKey}:delete`)
      const relation_list = auth.hasPermi(`cqPe:bsNode:${deptKey}:relation:list`)
      const dept1 = auth.hasPermi(`cqPe:bsNode:dept1:list`)
      const dept2 = auth.hasPermi(`cqPe:bsNode:dept2:list`)
      return { edit, add, remove, relation_list, dept1, dept2 }
    }
  },
  dicts: ['cq_random_type', 'cq_pe_frequency_unit', 'cq_pe_belong_system', 'cq_pe_generation_type', 'cq_pe_dimension'],
  created() {
    this.init()
  },
  methods: {
    init() {
      if (!this.showBtns.dept1 && this.showBtns.dept2) {
        this.queryParams.rdcType = 'DEPT2'
      } else if (!this.showBtns.dept1 && !this.showBtns.dept2) {
        this.$message.warning('当前用户暂无检查类型节点访问权限。')
        return
      }
      this.getList()
    },
    showEditDialog() {
      this.showEditItem = true
      this.$refs.EditItem.form.rdcType = this.queryParams.rdcType
    },
    showRelatedItem(row) {
      this.$refs.checkItemRef.open(row)
    },
    async getList() {
      this.listLoading = true
      const res = await listBsNode(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return
      const data = res.rows || []
      data.map(i => {
        i.removeLoading = false
        i.isEnableLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    async toggleIsEnable(item) {
      const name = !item.isEnable ? '启用' : '停用'
      const res1 = await this.$confirm(`确认${name}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      const opt = { ...item, isEnable: !item.isEnable }
      item.isEnableLoading = true
      const res = await updateBsNode(opt).catch(() => false)
      item.isEnableLoading = false
      if (res === false) return

      this.$message.success(`已${name}`)
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.businessType} - ${dictFilter(item.belongSystem, this.dict.type.cq_pe_belong_system)}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delBsNode(item.bsNodeId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
